package com.work.lxykeeper.module.pagemine;

import android.text.TextUtils;
import android.view.View;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.databinding.ActivityPagemineResetpasswordBinding;
import com.work.lxykeeper.httpretrofit.HttpStatus;
import com.work.lxykeeper.httpretrofit.MyCallback;
import com.work.lxykeeper.httpretrofit.PostBody;
import com.work.lxykeeper.httpretrofit.RetrofitManager;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * author: Aly
 * date :2018/12/7 13:39
 * version:1.0
 * description:重置密码
 */
public class ResetPasswordActivity extends BaseActivity {

    private ActivityPagemineResetpasswordBinding binding;

    @Override
    protected View getLayoutView() {
        binding = ActivityPagemineResetpasswordBinding.inflate(getLayoutInflater());
        return null;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("修改密码");
        ViewUtils.setOnClickListeners(this, binding.tvNext);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.tv_next:
                //确认
                if (TextUtils.isEmpty(binding.etPassword.getText().toString())) {
                    UIUtils.showToast("请输入原密码");
                    return;
                }
                if (TextUtils.isEmpty(binding.etPassword2.getText().toString())) {
                    UIUtils.showToast("请输入新密码");
                    return;
                }
                showLoaddialog();
                changePassword(binding.etPassword.getText().toString(), binding.etPassword2.getText().toString());
                break;
        }
    }

    //修改密码
    private void changePassword(String oldPassword, String newPassword) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", SPutils.get(Ckey.UID));
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);

        Call<HttpStatus> call = RetrofitManager.getInstance().changePassword(PostBody.toBody(map));
        call.enqueue(new MyCallback<HttpStatus>() {
            @Override
            public void onSuccess(HttpStatus response) {
                UIUtils.showToast(response.getMsg());
                finish();
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast(errMsg);
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });
    }

}
